<?php 
namespace app\api\controller;

use think\facade\Db;



/**
 * 
 *  订单对接接口
 * 
 */
class OrderkhController extends CommonController
{
	
    
     protected $url = "http://120.46.211.32:9523";

     protected $app_key = "edd979eada1266c0769bb88249adadba";

     protected $app_secret = "x5iX2UAVPRfST9hZriHn9lAyUYwHOIsx";


     protected $store_id=1;



     public function list(){

     	$limit=$this->request->post("limit",10);

     	$path="/api/order/list";

     	$url=$this->url.$path;


     	$sign=get_kh_sign($this->app_key,$this->app_secret);  

     	$Authorization=$this->app_key."/".time()."/".$sign;

     	$res=http_get($url,$Authorization);

        return $res;

     	$data=json_decode($res,true);


     	if($data['code'] == 200){

     		 $rows=$data['data'];

     		 foreach ($rows as $k => $r) {

     		 	 $orderdata=$r['order_goods'];

     		 	 foreach ($orderdata as $key => $ord) {

     		 	 	 $ordergoods=[];


     		 	 	 $ordergoods['store_ddbh']=$r['order_number'];
     		 	 	 $ordergoods['store_good_code']=$ord['distributors_goods_code'];
     		 	 	 $ordergoods['goods_name']=$ord['goods_name'];
     		 	 	 $ordergoods['goods_specification']=$ord['goods_specification'];
     		 	 	 // $ordergoods['distribution_price']=$ord['distribution_price'];
     		 	 	 $ordergoods['goods_price']=$ord['goods_price'];
     		 	 	 $ordergoods['manufacturer']=$ord['manufacturer'];
     		 	 	 $ordergoods['goods_quantity']=$ord['goods_quantity'];


     		 	 	 Db::connect("mysql2")->table("cd_store_orderdata")->insert($ordergoods);
     		 	 }

     		 	 $order=[];

     		 	 $order['store_ddbh']=$r['order_number'];
     		 	 $order['store_erpid']=$r['purchaser']['unique_code'];
     		 	 $order['purchaser_name']=$r['purchaser']['purchaser_name'];
     		 	 $order['order_amount']=$r['order_amount'];
     		 	 $order['created_at']=$r['created_at'];
     		 	 $order['remarks']=$r['remarks'];
     		 	 $order['shr']=$r['salesman']['real_name'];
     		 	 $order['shdh']=$r['salesman']['mobile'];
     		 	 $order['department_id']=$r['department']['id'];
     		 	 $order['department_name']=$r['department']['name'];


     		 	Db::connect("mysql2")->table("cd_store_order")->insert($order);
     		 	 

     		 }


     		 return json(['code'=>200,'msg'=>"success"]);

     	}else{
     	    return json(['code'=>$data['code'],'msg'=>$data['msg']]);
     	}


     

     }


     public function get_back(){


     	    $store_ddbh=$this->request->post("store_ddbh");

         
	        $path="/api/order/receive_feedback";

	     	$url=$this->url.$path;


	     	$sign=get_kh_sign($this->app_key,$this->app_secret);  

	     	$Authorization=$this->app_key."/".time()."/".$sign;


	     	$value=[];
	     	$arr=[];

	     	$arr[0]=$store_ddbh;

	     	$value['order_numbers']=$arr;

	     	$value=json_encode($value);


	     	$res=http_post_json($url,$value,$Authorization);


	     	return $res;



     }

       /**
        *  出库回写
        * @return [type]
        */
       public function feedback(){

       	  $path="/api/order/feedback";

       	  	$url=$this->url.$path;


	     	$sign=get_kh_sign($this->app_key,$this->app_secret);  

	     	$Authorization=$this->app_key."/".time()."/".$sign;


       }

       /**
        *  订单状态回写 （取消的订单） 
        * @return [type]
        */
       public function status_feedback(){


       	  $path="/api/order/status_feedback";
       	  	$url=$this->url.$path;
       	    $sign=get_kh_sign($this->app_key,$this->app_secret);  

	     	$Authorization=$this->app_key."/".time()."/".$sign;

       }

       /**
        *  订单完成的回写，（出库） 
        * @return [type]
        */
       public function completed_feedback(){

       }

       /**
        *
        * 库存同步 ，依照批号
        * 
        * @return [type]
        */
       public function stock_feedback(){



       }



}

 ?>